package phapp.springSecurity;

import java.util.*;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import javacommon.util.JsonUtil;
//import com.jw.system.model.system.Permission;
//import com.jw.system.model.system.Role;
//import com.jw.system.model.system.User;
//import com.jw.system.service.organiza.DataOrganizationsManager;
//import com.jw.system.service.system.PermissionManager;
import phapp.model.system.*;
import phapp.service.system.*;


@Transactional(readOnly = true)
public class UserDetailServiceImpl implements UserDetailsService
{
   private MenuManager menuManager;
   private SysUserManager sysUserManager;
   
   public MenuManager getMenuManager()
   {
      return menuManager;
   }

   public void setMenuManager(MenuManager menuManager)
   {
      this.menuManager = menuManager;
   }

   public SysUserManager getSysUserManager()
   {
      return sysUserManager;
   }

   public void setSysUserManager(SysUserManager sysUserManager)
   {
      this.sysUserManager = sysUserManager;
   }

   /**
    * 实现UserDetailsService的loadUserByUsername接口
    */
   public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException
   {
      SysUser loginSysuser = null;
      List<SysUser> listUser = null;
      SpringAuthUser userDetail = null;
      boolean enabled = true;
      boolean accountNonLocked = true;
      boolean accountNonExpired = true;
      boolean credentialsNonExpired = true;
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      mapQuery.clear();
      mapQuery.put("userAccount", userName);
      listUser = sysUserManager.findList(mapQuery);
      
      if(listUser != null && listUser.size() == 1)
         loginSysuser = listUser.get(0);
      
      if(loginSysuser == null)
         throw new UsernameNotFoundException("登陆用户不存在：" + userName);
      
      enabled = (loginSysuser.getUserStatus().intValue() == 1) ? true : false;
      Collection<? extends GrantedAuthority> grantedAuths = obtainGrantedAuthorities(loginSysuser);
      
      userDetail = new SpringAuthUser(loginSysuser.getUserAccount(), loginSysuser.getUserPasswd(), 
               enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths);
      userDetail.setUserId(loginSysuser.getId().toString());
      userDetail.setUserName(loginSysuser.getUserName());
      
      return userDetail;
   }

   private Collection<? extends GrantedAuthority> obtainGrantedAuthorities(SysUser user)
   {
      Map mapQuery = null;
      Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
      
      //加载用户权限菜单
      List<Menu> listMenu = menuManager.findList(mapQuery);//menuManager.getPermissionByUserId(user.getId());
      if(listMenu != null)
      {
         for(Menu menuItem : listMenu)
         {
            String strMenuID = menuItem.getId().toString();
            if(strMenuID != null)
            {
               authSet.add(new GrantedAuthorityImpl(strMenuID));
            }
         }
      }
      
      return authSet;
   }



}
